<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\App;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use app\common\model\Pro;
use app\common\model\Company;
use app\common\tool\excel\Export;
use think\response\Json;

class CheckList extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\CheckList';
    
    //关联模型
    protected $hasOne = ['pro', 'company','oneuser','twouser','threeuser'];

    public function index()
    {
        return $this->setTable();
    }

    //自动构建table,按需修改
    public function setTable()
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'protitle', 'title' => '所属项目','templet' => '<div>{{d.pro.title}}</div>'])
            ->addRow(['field' => 'companytitle', 'title' => '所属场所/单位名','templet' => '<div>{{d.company.title}}</div>'])
            ->addRow(['field' => 'oneusernick', 'title' => '初检','templet' => '<div>{{d.one_uid !=0 ?d.oneuser.nickname:"-"}}</div>'])
            ->addRow(['field' => 'one_time', 'title' => '初检时间'])
            ->addRow(['field' => 'twousernick', 'title' => '二检','templet' => '<div>{{d.two_uid !=0 ?d.twouser.nickname:"-"}}</div>'])
            ->addRow(['field' => 'two_time', 'title' => '二检时间'])
            ->addRow(['field' => 'threeusernick', 'title' => '三检','templet' => '<div>{{d.three_uid !=0 ?d.threeuser.nickname:"-"}}</div>'])
            ->addRow(['field' => 'three_time', 'title' => '三检时间'])
            ->addRow(['field' => 'types', 'title' => '检查状态', 'templet' => '#CheckTpl'])
            ->addRow(['field' => 'create_time', 'title' => '创建时间'])
            
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['delete'])
            ->addTopButtons(['deleteAll'])
            ->setSearch($this->setSearch())
            ->addExtraHtml(['check'])
            ->fetch('checklist/index');
    }
    //自动构建Search，按需修改
    public function setSearch()
    {
        $pro=Pro::proSelect();
        return TableFront::getInstance()
        ->setSearch(['company_id', '所属场所/单位名', 'input', '', '\app\common\model\Company', 'company_name,id'])
        ->setSearch(['pro_id', '所属项目', 'select', $pro])
        ->setSearch(['types', '检查状态', 'select', [1=>'初检',2=>'二检',3=>'三检']]);
    }
    //导出为excel文件
    public function exportExcel(){
        $param = $this->request->param();
        $fileName="检查数据-".date('YmdHis').'.xlsx';
        $headArr = ['id','项目','公司名','uid','types'];
        $ids=explode(',',$param['data']);
        $list=$this->model::whereIn('id', $ids)->select()->toArray();
        foreach($list as $k=>$v){
            $pro=Pro::where(['id'=>$v['pro_id']])->value('title');
            $company=Company::where(['id'=>$v['company_id']])->value('company_name');
            $data[]=[$v['id'],$pro,$company,0,$v['types']];
        }
        //print('<pre>');print_r($data);die;
        Export::excelExport($headArr, $data,$fileName);
    }
    //导入为excel文件
    public function importExcel():Json{
        $param = $this->request->param();
        $data=htmlspecialchars_decode($param['persons']);
        $list=json_decode($data,true);
        foreach($list as $k=>$v){
            $find=$this->model::where(['id'=>$v['id']])->find();
            if($v['types']==1){
                $this->model::where(['id'=>$v['id']])->update(['two_uid'=>$v['uid'],'types'=>2,'two_status'=>1]);
            }else if($v['types']==2){
                if($find['two_time']!=0){
                    $this->model::where(['id'=>$v['id']])->update(['three_uid'=>$v['uid'],'types'=>3,'three_status'=>1]);
                }
            }
        }
        return json(['code'=>1,'msg'=>'导入成功！']);
    }
     
}
